package com.aruba.canvasapplication;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * 搜索动画效果
 */
public class SearchAnimView extends View {
    //线的颜色
    private int strokeColor;
    private Paint mPaint = new Paint();
    //扇形圆的中心和圆的半径
    private float centerX, centerY, radius;
    //记录动画进度
    private float ratio;
    //搜索图标下划线长度
    private float searchLineWidth;
    //搜索图标消失系数
    private float factor = 0.3f;
    //下划线长度
    private float lineWidth;

    public SearchAnimView(Context context) {
        this(context, null);
    }

    public SearchAnimView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        strokeColor = Color.BLACK;
        init();
    }

    public SearchAnimView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init() {
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(strokeColor);
        mPaint.setStrokeWidth(10);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

        radius = 50;
        searchLineWidth = 80;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        //自动缩放
        if (height < radius * 2 + searchLineWidth + mPaint.getStrokeWidth() * 3) {
            float scale = height / (radius * 2 + searchLineWidth + mPaint.getStrokeWidth() * 3);
            radius *= scale;
            searchLineWidth *= scale;
            mPaint.setStrokeWidth(mPaint.getStrokeWidth() * scale);
        }

        //搜索图标放置位置，有个偏移量可以显示完全
        centerX = width - radius - searchLineWidth + mPaint.getStrokeWidth() * 3;
        centerY = height / 2.0f;

        //有个偏移量可以显示完全
        lineWidth = width - mPaint.getStrokeWidth() * 2;

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.save();
        canvas.rotate(45, centerX, centerY);
        //画搜索图标的圆，在动画0-0.5内角度变为0，圆心平移radius
        if (ratio <= factor) {
            RectF rectF = new RectF(centerX - radius + radius * ratio * 1 / factor, centerY - radius, centerX + radius + radius * ratio * 1 / factor, centerY + radius);
            canvas.drawArc(rectF, 0, 360 * (1 - ratio * 1 / factor), false, mPaint);
        }
        //画搜索图标的线,在动画0-1内长度变为0
        Path path = new Path();
        float moveX = centerX + radius + radius * ratio * 1 / factor;
        float lineX = centerX + radius + searchLineWidth;
        moveX = moveX > lineX ? lineX : moveX;
        path.moveTo(moveX, centerY);
        path.lineTo(lineX, centerY);
        canvas.drawPath(path, mPaint);

        canvas.save();
        canvas.rotate(-45, lineX, centerY);
        //画搜索图标的下划线
        Path bootomPath = new Path();
        bootomPath.moveTo(lineX, centerY);
        bootomPath.lineTo(lineX - lineWidth * ratio, centerY);
        canvas.drawPath(bootomPath, mPaint);

        canvas.restore();
        canvas.restore();

    }

    private ValueAnimator valueAnimator;
    //动画执行时间
    private int duration = 1000;

    public void startAnim() {
        if (valueAnimator == null) {
            valueAnimator = ValueAnimator.ofFloat(1);
            valueAnimator.setDuration(duration);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ratio = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
        }

        if (!valueAnimator.isRunning()) valueAnimator.start();
    }

    public void reverseAnim() {
        if (valueAnimator != null) {
            valueAnimator.reverse();
        }
    }
}
